<?php


namespace App\Imports;

use App\Models\CompanyUser;
use App\Models\PartCompany;
use App\Models\PartCompanyUser;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\Role;
use App\Models\User;
use App\Services\CompanyService;
use Maatwebsite\Excel\Concerns\ToModel;

class UsersImport implements ToModel
{
    public function model(array $row)
    {
        if ($row[0] && is_numeric($row[0])) {
            //创建用户
            $userInfo = User::query()->where('phone', $row[6])->first();
            if (empty($userInfo)) {
                $user              = new User();
                $user->nickname    = format_str($row[8]);
                $user->phone       = format_str($row[6]);
                $user->password    = format_str($row[7]);
                $user->no_hash_pwd = format_str($row[7]);
                $user->creator_id  = user_id();
                $user->status      = 1;
                $user->save();
                $userId = $user->id;
            } else {
                $userId = $userInfo->id;
            }
            //找到项目
            $project = Project::query()->where('document_number', format_str($row[3]))->first();
            if (!empty($project)) { //如果项目存在
                $projectUserInfo = ProjectUser::query()->where('project_id', $project->id)->where('user_id', $userId)->first();
                if (empty($projectUserInfo)) {
                    $projectUser             = new ProjectUser();
                    $projectUser->user_id    = $userId;
                    $projectUser->project_id = $project->id;
                    $projectUser->creator_id = user_id();
                    $projectUser->save();
                }
                //找到公司
                $companyId = $project->company_id;
                //公司和角色
                CompanyService::addCompanyRoles($companyId);
                //用户参建单位
                $partCompanyId = PartCompany::query()->where('project_id', $project->id)->where('title', format_str($row[5]))->value('id');
                if (!$partCompanyId) { //创建参建单位
                    $partCompany               = new PartCompany();
                    $partCompany->company_id   = $companyId;
                    $partCompany->project_id   = $project->id;
                    $partCompany->type         = 3;
                    $partCompany->title        = format_str($row[5]);
                    $partCompany->contract_num = format_str($row[4]); //合同号
                    $partCompany->match_units  = '';
                    $partCompany->creator_id   = user_id();
                    $partCompany->save();
                    $partCompanyId = $partCompany->id;
                }
                $partCompanyUserInfo = PartCompanyUser::query()->where('part_company_id', $partCompanyId)->where('user_id', $userId)->first();
                if (empty($partCompanyUserInfo)) { //如果用户和参见单位还没关联
                    $partCompanyUser                  = new PartCompanyUser();
                    $partCompanyUser->part_company_id = $partCompanyId;
                    $partCompanyUser->user_id         = $userId;
                    $partCompanyUser->creator_id      = user_id();
                    $partCompanyUser->save();
                }

                //找到公司的归档员角色
                $roleId          = Role::query()->where('company_id', $companyId)->where('title', '档案员')->value('id');
                $companyUserInfo = CompanyUser::query()
                    ->where('company_id', $companyId)
                    ->where('user_id', $userId)
                    ->where('role_id', $roleId)
                    ->first();
                if (empty($companyUserInfo)) {
                    $companyUser             = new CompanyUser();
                    $companyUser->company_id = $companyId;
                    $companyUser->user_id    = $userId;
                    $companyUser->role_id    = $roleId ?? 0;
                    $companyUser->is_admin   = 0;
                    $companyUser->creator_id = user_id();
                    $companyUser->status     = 1;
                    $companyUser->save();
                }
            } else {
                info($row);
            }
        }
    }

    //检验格式
    public function checkFormat($row)
    {

    }

}
